package com.highspeed.bdk.gateway.socket.analyze.message.gui.resolver;

import com.highspeed.bdk.gateway.Util;
import com.highspeed.bdk.gateway.socket.analyze.message.gui.base.MessageResolver;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class DeviceNodeFbDataResolver0x03 extends MessageResolver {

    @Override
    protected Map doResolveBody(byte[] response) {

        Map result = new HashMap();
        Integer physicalId = Util.getInt(response, getOffset());
        offset4();
        logger.info("physicalId........." + physicalId);
        result.put("physicalId", physicalId);
        result.put("slotNum", ((physicalId >> 2) & 63));
        result.put("cpuType", (physicalId >> 12) & 15);
        logger.info("cpuType......" + result.get("cpuType"));


        short rioNetId = Util.getShort(response, getOffset());
        offset2();
        logger.info("rioNetId.........:" + rioNetId);
        result.put("rioNetId", rioNetId);
        result.put("cpuName", rioNetId);

        short portEntrySize = Util.getShort(response, getOffset());
        offset2();
        logger.info("portEntrySize..............." + portEntrySize);
        result.put("portEntrySize", portEntrySize);

        short oneRouteLen = Util.getShort(response, getOffset());
        offset2();
        logger.info("oneRouteLen........." + oneRouteLen);
        result.put("oneRouteLen", oneRouteLen);

        short reserved = Util.getShort(response, getOffset());
        offset2();
        logger.info("reserved..............." + reserved);

        for (int nodeSize = 0; nodeSize < portEntrySize; nodeSize++) {
            Short m_inPortId = Util.getShort(response, getOffset());
            offset2();
            logger.info("m_inPortId..........." + m_inPortId);

            Short m_dstID = Util.getShort(response, getOffset());
            offset2();
            logger.info("m_dstID..........." + m_dstID);

            Short outPorts = Util.getShort(response, getOffset());
            offset2();
            logger.info("outPorts..........." + outPorts);

            Short nextHops = Util.getShort(response, getOffset());
            offset2();
            logger.info("nextHops..........." + nextHops);

            short[] m_outPortIds = new short[outPorts];
            short[] m_nextHops = new short[nextHops];
            for (short i = 0; i < outPorts; i++) {
                Short outport = Util.getShort(response, getOffset());
                offset2();
                m_outPortIds[i] = outport;
            }
            for (short i = 0; i < nextHops; i++) {
                Short nextHop = Util.getShort(response, getOffset());
                offset2();
                m_nextHops[i] = nextHop;
            }
            logger.info("outPorts=" + outPorts + ",m_outPortIds=" + Arrays.toString(m_outPortIds) + "......nextHops=" + nextHops + ",m_nextHops=" + Arrays.toString(m_nextHops));
        }
        getContainer().put("cpu", result);
        return getContainer();
    }
}
